<?php

namespace HelloAdmin\Controller;

class MemberController extends CommonController {

    private $Members;

    public function __construct() {
        parent::__construct();
        $this->Members = new \Common\Model\MembersModel();
    }

    public function index() {

        $this->display('list');
    }
    public function listRes(){
        $search = I('post.search');
        $field = I('post.field');
        $d_a = I('post.order');
        if(empty($search)){
           $where = '1=1';
        }else{
            $where = "`login_account` like '{$search}%'";
        }
        $order = '';
        if(!empty($field) && $d_a){
            $order = "`{$field}` $d_a";
        }
        $data = $this->Members->getList($where,$order,$this->offset,$this->limit);
        echo (json_encode($data));
    }


    public function operateDisabled(){
        $member_id = I('post.id');
        $disabled = I('post.disabled');
        if($member_id > 0 && $disabled > 0){
            if($this->Members->openClose($member_id,$disabled)){
                echo 'success';exit;
            }
        }
    }

    //重置会员密码
    public function newPassword(){
        $member_id = I('post.id');
        $pass = I('post.password');
        if($member_id > 0 && $pass && IS_AJAX){
            if(\Common\Library\COM\MemberDo::newPassword($member_id, $pass)){
                echo 'success';exit;
            }
        }
    }

    public function memberLv(){

        $this->display('memberlv');
    }

    public function memberLvlist(){
        $search = I('post.search');
        $field = I('post.field');
        $d_a = I('post.order');
        if(empty($search)){
           $where = '1=1';
        }else{
            $where = "`login_account` like '{$search}%'";
        }
        $order = '';
        if(!empty($field) && $d_a){
            $order = "`{$field}` $d_a";
        }
        $MemberLv = new \Common\Model\MemberLvModel();
        $data = $MemberLv->getList($where,$order,$this->offset,$this->limit);
        echo (json_encode($data));
    }

    public function addMemberLv(){
        $Lv = new \Common\Model\MemberLvModel();
        if(empty($_POST)){

            $this->assign('lvs',$Lv->getList());
            $this->display();exit;
        }
        $data = array(
            'name'=>I('post.name'),
            'pre_id'=>I('post.pre_id'),
            'default_lv'=>I('post.default_lv'),
            'point'=>I('post.point'),
            'lv_remark'=>I('post.lv_remark'),
        );
        if($Lv->add($data)){
            $this->layerIfreamRe('添加成功');
        }
    }

    /**
     * 会员等级相关信息跟新
     */
    public function lvUpdate(){
        $field = I('post.field');
        $value = I('post.value');
        $member_lv_id = I('post.id');
        if($member_lv_id > 0 && $field && $value){
            $Lv = new \Common\Model\MemberLvModel();
            if($Lv->updateById($member_lv_id,array($field=>$value))){
                echo 'success';exit;
            }

        }
    }

    public function delLv(){
        $ids = I('post.ids');
        if(!empty($ids)){
            $Lv = new \Common\Model\MemberLvModel();
            if($Lv->delLv($ids)){
                echo 'success';exit;
            }
        }

    }







}
